package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convent.AuthRoleConvent;
;
import com.jingdianjichi.auth.application.dto.AuthRoleDto;

import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRoleBo;

import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {


    @Resource
    AuthRoleDomainService authRoleDomainService;

    /**
     * 角色添加
     *
     * @return
     * @paramauthRoleDto
     */
    @PostMapping("add")
    public Result Register(@RequestBody AuthRoleDto authRoleDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto,{}", JSON.toJSONString(authRoleDto));
            }

            Preconditions.checkNotNull(authRoleDto.getRoleName(), "角色名称不能为空");
            Preconditions.checkArgument(!StringUtils.isEmpty(authRoleDto.getRoleKey()), "角色Key不能为空");


            AuthRoleBo authRoleBo = AuthRoleConvent.INSERT.conventDtoToBo(authRoleDto);

            authRoleDomainService.add(authRoleBo);

            return Result.ok();
        } catch (Exception e) {
            log.error("RoleController.add.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }


    /**
     * 角色信息修改
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDto authRoleDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto,{}", JSON.toJSONString(authRoleDto));
            }

            Preconditions.checkNotNull(authRoleDto.getId(), "角色id不能为空");


            AuthRoleBo authRoleBo = AuthRoleConvent.INSERT.conventDtoToBo(authRoleDto);

            Boolean update = authRoleDomainService.update(authRoleBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("RoleController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage() + "角色信息更新失败");
        }

    }

    /**
     * 角色启用和禁用
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("updateStatus")
    public Result<Boolean> updateStatus(@RequestBody AuthRoleDto authRoleDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto,{}", JSON.toJSONString(authRoleDto));
            }

            Preconditions.checkNotNull(authRoleDto.getRoleName(), "角色名称不能为空");


            AuthRoleBo authRoleBo = AuthRoleConvent.INSERT.conventDtoToBo(authRoleDto);

            Boolean update = authRoleDomainService.update(authRoleBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("RoleController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage() + "角色启用禁用失败");
        }

    }

    /**
     * 角色信息删除
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("delect")
    public Result<Boolean> delect(@RequestBody AuthRoleDto authRoleDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delect.dto,{}", JSON.toJSONString(authRoleDto));
            }

            Preconditions.checkNotNull(authRoleDto.getId(), "角色id不能为空");


            AuthRoleBo authRoleBo = AuthRoleConvent.INSERT.conventDtoToBo(authRoleDto);

            Boolean update = authRoleDomainService.delect(authRoleBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("RoleController.delect.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage() + "角色信息更新失败");
        }

    }

}


